<?php
use Fox\Common\Customer;
use Fox\Html\Input;
use Fox\Common\Customer\Type;
use Doctrine\DBAL\Connection;
use Fox\Generator;
use Fox\MaterialManagement\Material\Hierarchy;
use Fox\Html\DropList;
use Fox\Html\Screen;
use Fox\MaterialManagement\Material;
use Fox\Html\TextArea;

global $hierarchyLabel;
$xview = Xview::instance();
$id = Xview::instance()->val("id");

$excluded = array();

$searchHelp = new Fox\Html\SearchHelp("MaterialBasic");
$searchHelp->getSelBlock()->item("materialid")->id("t.materialid");
$searchHelp->setMultiple(new Fox\Html\Button('select',"ReloadFromSelectLines('#b_GRMaterialBasic','MC_tComponents','?mod=material&act=boms&sac=components','App.close(\'sh:MaterialBasic\')','InputSerialize(\'eMaterial\')');"));
$searchHelp->switchToObjectMode();
$ilink = $xview->currentURL();

$searchHelp->getSelBlock()->add(new TextArea("excluded"))
							->excluded()
							->hidden();
$searchHelp->getSelBlock()->add(new Fox\Html\Input("id",false,$id))
							->excluded()
							->hidden();
$searchHelp->getSelBlock()->item("material_label")->id("label");
$searchHelp->getSelBlock()->item("material_label")->size(Input::SIZE_LARGE);
$searchHelp->width(700);

$hiearchies = Hierarchy::loadTree();
foreach($hiearchies as $hierarchy)
{
	$parentid = $hierarchy['parentid'];
	if(!empty($parentid) && !isset($hiearchyTree[$parentid]))
		continue;

	if(empty($parentid))
	{
		$hierarchyLabel[$hierarchy['code']] = $hierarchy['label'];
		$hiearchyTree[$hierarchy['id']] =  $hierarchy['code'];
	}
	else 
	{
		$code = $hiearchyTree[$parentid];
		$code .= $hierarchy['code'];
		$hierarchyLabel[$code] = $hierarchyLabel[$hiearchyTree[$parentid]]." / ".$hierarchy['label'];
	}
}
$searchHelp->getSelBlock()->item("mat_hierarchy")->options($hierarchyLabel);
$searchHelp->getSelBlock()->item("mat_hierarchy")->id("hierarchy");
// Load already selected Product
$blItems = Xview::instance()->component("material", "components"); 
$data = $blItems->item("tComponents")->get();
if(is_array($data))
{
	foreach($data as $line)
	{
		if(isset($line['componentid']))
			$Ids[] = $line['componentid'];
	}
	if(!empty($id))
		$Ids[] = $id;
	if(isset($Ids))
	{
		$excluded = new \Fox\Dao\SqlFragment("t.materialid NOT IN (?) ",array($Ids),array(Connection::PARAM_STR_ARRAY));
		$searchHelp->getSelBlock()->item("excluded")->default = addslashes(json_encode($Ids));
	}
}
else
{
	$values = Xview::instance()->val("excluded");
	if(!empty($values))
	{
		$Ids = json_decode(stripcslashes($values),true);
		$excluded = new \Fox\Dao\SqlFragment("t.materialid NOT IN (?) ",array($Ids),array(Connection::PARAM_STR_ARRAY));
	}
}
$searchHelp->grid()->getFieldcat()->field("mat_hierarchy")
				->width(10)
				->id("label");
$searchHelp->grid()->getFieldcat()->field("material_label")
				->width(20)
				->id("label");
// Filter By Customer Type 
if(isset($_POST['hierarchy']))
{
	$fragments[] = $searchHelp->screen()->fragments();
	$finder = new Material();
	if(isset($excluded))
		$fragments[] = $excluded;

	$res = $finder->find(Fox\Dao\SqlFragment::combine($fragments),array("label" => false),array(Material::$JOIN_TEXTS));
	$searchHelp->grid()->getFieldcat()->field("material_label")
				->width(50)
				->id("label");
	$searchHelp->grid()->getFieldcat()->field("mat_hierarchy")->id("hierarchy");
	
	$searchHelp->grid()->fieldcat()->setGenerators(array(
		'mat_hierarchy' => Fox\Generator::useFunction(function ($data,$key){
		global $hierarchyLabel;

		if(isset($hierarchyLabel[$data->hierarchy]))
			return $hierarchyLabel[$data->hierarchy];
		else 
			return $data->hierarchy;
	
	})
		
	));
	
	
	$searchHelp->grid()->setDatas($res);
	echo $searchHelp->grid()->Display();
}
else
{
	echo $searchHelp->display();
}
